package com.losl.common.exception;

import com.losl.common.util.SpringUtils;
import com.losl.common.util.StringUtils;
import lombok.Getter;
import org.apache.xmlbeans.impl.store.Locale;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;

/**
 * 基础异常
 * @author Maxwell
 */
@Getter
public class BaseException extends RuntimeException{
    private static final long serialVersionUID = 1L;

    /**
     * 所属模块
     */
    private String module;

    /**
     * 错误码
     */
    private String code;

    /**
     * 错误码对应的参数
     */
    private Object[] args;

    /**
     * 错误消息
     */
    private String defaultMessage;

    public BaseException(String module, String code, Object[] args, String defaultMessage) {
        this.module = module;
        this.code = code;
        this.args = args;
        this.defaultMessage = defaultMessage;
    }

    public BaseException(String module, String code, Object[] args)
    {
        this(module, code, args, null);
    }

    public BaseException(String module, String defaultMessage)
    {
        this(module, null, null, defaultMessage);
    }

    public BaseException(String code, Object[] args)
    {
        this(null, code, args, null);
    }

    public BaseException(String defaultMessage)
    {
        this(null, null, null, defaultMessage);
    }

    @Override
    public String getMessage() {
        String message=null;
        if (StringUtils.isNotEmpty(code)){
            MessageSource messageSource = SpringUtils.getBean(MessageSource.class);
            message = messageSource.getMessage(code, args, LocaleContextHolder.getLocale());
        }
        if (message == null){
            message=defaultMessage;
        }
        return message;
    }
}
